function DbHelper() {
	var api = {};

	var dbVersion = 1;

	api.checkVersion = function() {
		var db = Ti.Database.install('/database/songs.db', 'songs');
		var rs = db.execute('SELECT version from version limit 1');
		while (rs.isValidRow()) {
			if (rs.fieldByName('version') < dbVersion) {
				db.remove();
				db.close();
				db = Ti.Database.install('/database/songs.db', 'songs');
				db.close();
			} else {
				rs.close();
				db.close();
			}
			break;
		}
	}

	api.getRows = function(lang) {
		var db = Ti.Database.install('/database/songs.db', 'songs');

		var tableData = [];
		var index = [];

		var prevHeader = "";
		var header = "";
		var count = 0;

		var sort = lang == "mal" ? "title_ml" : "title_en";
		var title;

		rs = db.execute('SELECT song_id, title_en, title_ml, filename_ml, filename_en FROM songs order by ' + sort);
		while (rs.isValidRow()) {
			title = lang == "mal" ? rs.fieldByName('title_ml') : rs.fieldByName('title_en');
			header = title.substr(0, 1);

			if (prevHeader != header) {
				index.push({
					title : header,
					index : count
				});
				tableData.push({
					ml : rs.fieldByName('filename_ml'),
					en : rs.fieldByName('filename_en'),
					title : title,
					title_ml: rs.fieldByName('title_ml'),
					filter : rs.fieldByName('title_en'),
					header : header
				});
				prevHeader = header;
			} else {
				tableData.push({
					ml : rs.fieldByName('filename_ml'),
					en : rs.fieldByName('filename_en'),
					title : title,
					title_ml: rs.fieldByName('title_ml'),
					filter : rs.fieldByName('title_en')
				});
			}
			count++;
			rs.next();
		}
		
		db.close();

		return {
			data : tableData,
			index : index
		};
	};

	return api;
};

module.exports = DbHelper;
